<%@ page import="org.example.entity.Content" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>内容管理 - 内容评级平台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>

        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --accent: #4cc9f0;
            --dark: #1d3557;
            --darker: #0d1b2a;
            --light: #f8f9fa;
            --gray: #8d99ae;
            --success: #4caf50;
            --warning: #ff9800;
            --danger: #f44336;
            --card-bg: rgba(29, 53, 87, 0.8);
            --transition: all 0.3s ease;
        }
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        body {
            background: linear-gradient(135deg, var(--darker) 0%, var(--dark) 100%);
            color: var(--light);
            min-height: 100vh;
            background-attachment: fixed;
            line-height: 1.6;
        }
        header {
            background-color: rgba(13, 27, 42, 0.95);
            backdrop-filter: blur(10px);
            padding: 1rem 2rem;
            position: sticky;
            top: 0;
            z-index: 1000;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
        .header-container {
            max-width: 1400px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
        }
        .logo {
            display: flex;
            align-items: center;
            gap: 15px;
            text-decoration: none;
        }
        .logo-icon {
            font-size: 2.5rem;
            color: var(--accent);
        }
        .logo-text {
            font-size: 1.8rem;
            font-weight: 700;
            color: var(--light);
            letter-spacing: 1px;
        }
        .logo-text span {
            color: var(--accent);
        }
        .nav-links {
            display: flex;
            gap: 2rem;
            list-style: none;
        }
        .nav-links a {
            color: var(--light);
            text-decoration: none;
            font-weight: 500;
            font-size: 1.1rem;
            padding: 0.5rem 1rem;
            border-radius: 30px;
            transition: var(--transition);
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .nav-links a:hover, .nav-links a.active {
            background: rgba(67, 97, 238, 0.2);
            color: var(--accent);
        }
        .nav-links a i {
            font-size: 1.2rem;
        }
        .auth-buttons {
            display: flex;
            gap: 1rem;
        }
        .btn {
            padding: 0.7rem 1.5rem;
            border-radius: 30px;
            font-weight: 600;
            font-size: 1rem;
            cursor: pointer;
            transition: var(--transition);
            border: none;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .btn-primary {
            background: var(--primary);
            color: white;
        }
        .btn-primary:hover {
            background: var(--secondary);
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(67, 97, 238, 0.4);
        }
        .btn-outline {
            background: transparent;
            border: 2px solid var(--primary);
            color: var(--primary);
        }
        .btn-outline:hover {
            background: rgba(67, 97, 238, 0.1);
            transform: translateY(-2px);
        }
        .btn-danger {
            background: var(--danger);
            color: white;
        }
        .btn-danger:hover {
            background: #d32f2f;
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(244, 67, 54, 0.4);
        }
        .main-content {
            max-width: 1400px;
            margin: 2rem auto;
            padding: 0 2rem;
        }
        .dashboard-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
        }
        .section-title {
            font-size: 1.8rem;
            font-weight: 700;
            position: relative;
            padding-left: 1rem;
        }
        .section-title::before {
            content: "";
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 4px;
            background: var(--accent);
            border-radius: 2px;
        }
        .filter-controls {
            display: flex;
            gap: 1rem;
            margin-bottom: 2rem;
        }
        .filter-controls select, .filter-controls input {
            padding: 0.7rem 1rem;
            border-radius: 8px;
            background: rgba(255, 255, 255, 0.05);
            color: var(--light);
            border: 1px solid rgba(255, 255, 255, 0.1);
            font-size: 1rem;
        }
        .filter-controls select:focus, .filter-controls input:focus {
            outline: none;
            border-color: var(--accent);
            box-shadow: 0 0 0 2px rgba(76, 201, 240, 0.2);
        }
        .content-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 2rem;
        }
        .content-table th, .content-table td {
            padding: 1rem;
            text-align: left;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        .content-table th {
            background: rgba(67, 97, 238, 0.1);
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        .content-table tbody tr {
            background: var(--card-bg);
            transition: var(--transition);
        }
        .content-table tbody tr:hover {
            background: rgba(67, 97, 238, 0.15);
            transform: translateX(5px);
        }
        .content-table td img {
            max-width: 60px;
            max-height: 80px;
            border-radius: 4px;
        }
        .status-badge {
            padding: 0.3rem 0.7rem;
            border-radius: 20px;
            font-size: 0.9rem;
            font-weight: 500;
            display: inline-block;
        }
        .status-published {
            background: rgba(76, 175, 80, 0.2);
            color: #4caf50;
        }
        .status-draft {
            background: rgba(156, 39, 176, 0.2);
            color: #9c27b0;
        }
        .status-archived {
            background: rgba(121, 85, 72, 0.2);
            color: #795548;
        }
        .action-btn {
            padding: 0.5rem 0.8rem;
            border-radius: 4px;
            font-size: 0.9rem;
            text-decoration: none;
            color: white;
            margin-right: 0.5rem;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }
        .btn-edit {
            background: var(--primary);
        }
        .btn-edit:hover {
            background: var(--secondary);
        }
        .btn-delete {
            background: var(--danger);
        }
        .btn-delete:hover {
            background: #d32f2f;
        }
        .btn-publish {
            background: var(--success);
        }
        .btn-publish:hover {
            background: #388e3c;
        }
        .btn-archive {
            background: var(--warning);
        }
        .btn-archive:hover {
            background: #f57c00;
        }
        .pagination {
            display: flex;
            justify-content: center;
            gap: 0.5rem;
            margin-top: 2rem;
        }
        .pagination a {
            padding: 0.5rem 1rem;
            border-radius: 4px;
            background: rgba(255, 255, 255, 0.05);
            color: var(--light);
            text-decoration: none;
            transition: var(--transition);
        }
        .pagination a:hover, .pagination a.active {
            background: var(--primary);
        }


    </style>
</head>
<body>
<!-- 页头 -->
<jsp:include page="header.jsp" />
<div class="main-content">
    <div class="dashboard-header">
        <h1 class="section-title"><i class="fas fa-tachometer-alt"></i> 内容管理</h1>
        <a href="${pageContext.request.contextPath}/content/add" class="btn btn-primary"></i> 添加内容</a>
    </div>
    <!-- 过滤和搜索 -->
    <div class="filter-controls">
        <select id="typeFilter" name="type" class="form-control" onchange="filterContents()">
            <option value="">所有类型</option>
            <option value="movie" <%= request.getParameter("type") != null && request.getParameter("type").equals("movie") ? "selected" : "" %>>电影</option>
            <option value="book" <%= request.getParameter("type") != null && request.getParameter("type").equals("book") ? "selected" : "" %>>图书</option>
            <option value="music" <%= request.getParameter("type") != null && request.getParameter("type").equals("music") ? "selected" : "" %>>音乐</option>
        </select>
        <input type="text" id="searchInput" placeholder="搜索内容..." class="form-control">
        <button onclick="searchContents()" class="btn btn-primary"><i class="fas fa-search"></i> 搜索</button>
    </div>
    <!-- 内容列表 -->
    <div class="content-table-container">
        <table class="content-table">
            <thead>
            <tr>
                <th>封面</th>
                <th>标题</th>
                <th>类型</th>
                <th>描述</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <%
                List<Content> contents = (List<Content>) request.getAttribute("contents");
                if (contents != null && !contents.isEmpty()) {
                    for (Content content : contents) {
            %>
            <tr>
                <td>
                    <img src="<%= content.getCoverUrl() != null ? content.getCoverUrl() : "https://picsum.photos/60/80" %>" alt="封面">
                </td>
                <td><%= content.getTitle() %></td>
                <td><%= content.getType() %></td>
                <td><%= content.getDescription().length() > 50 ? content.getDescription().substring(0, 50) + "..." : content.getDescription() %></td>
                <td>
                            <span class="status-badge status-<%= content.getStatus() %>">
                                <%= content.getStatus().equals("published") ? "已发布" :
                                        content.getStatus().equals("draft") ? "草稿" : "已归档" %>
                            </span>
                </td>
                <td>
                    <a href="<%= request.getContextPath() %>/content/edit?id=<%= content.getId() %>" class="action-btn btn-edit"><i class="fas fa-edit"></i> 编辑</a>
                    <a href="javascript:void(0)" onclick="confirmDelete('<%= content.getId() %>')" class="action-btn btn-delete"><i class="fas fa-trash"></i> 删除</a>
                    <% if (!"published".equals(content.getStatus())) { %>
                    <a href="<%= request.getContextPath() %>/content/status?id=<%= content.getId() %>&status=published" class="action-btn btn-publish"><i class="fas fa-upload"></i> 发布</a>
                    <% } %>
                    <% if (!"archived".equals(content.getStatus())) { %>
                    <a href="<%= request.getContextPath() %>/content/status?id=<%= content.getId() %>&status=archived" class="action-btn btn-archive"><i class="fas fa-archive"></i> 归档</a>
                    <% } %>
                </td>
            </tr>
            <%
                }
            } else {
            %>
            <tr>
                <td colspan="6" style="text-align: center; padding: 3rem 0;">
                    <div style="display: flex; flex-direction: column; align-items: center;">
                        <i class="fas fa-folder-open" style="font-size: 3rem; color: var(--gray); margin-bottom: 1rem;"></i>
                        <p style="color: var(--gray);">暂无内容</p>
                        <a href="add-content.jsp" class="btn btn-primary mt-2"><i class="fas fa-plus"></i> 添加内容</a>
                    </div>
                </td>
            </tr>
            <%
                }
            %>
            </tbody>
        </table>
    </div>
    <!-- 分页 -->
    <div class="pagination">
        <%
            int currentPage = request.getAttribute("currentPage") != null ?
                    (Integer) request.getAttribute("currentPage") : 1;
            int totalPages = request.getAttribute("totalPages") != null ?
                    (Integer) request.getAttribute("totalPages") : 1;
            String type = request.getParameter("type") != null ?
                    request.getParameter("type") : "";
        %>
        <% if (currentPage > 1) { %>
        <a href="?page=1&type=<%= type %>"><i class="fas fa-angle-double-left"></i></a>
        <a href="?page=<%= currentPage - 1 %>&type=<%= type %>"><i class="fas fa-angle-left"></i></a>
        <% } %>
        <%
            int startPage = Math.max(1, currentPage - 2);
            int endPage = Math.min(totalPages, currentPage + 2);
            for (int i = startPage; i <= endPage; i++) {
        %>
        <a href="?page=<%= i %>&type=<%= type %>" <%= i == currentPage ? "class=\"active\"" : "" %>><%= i %></a>
        <%
            }
        %>
        <% if (currentPage < totalPages) { %>
        <a href="?page=<%= currentPage + 1 %>&type=<%= type %>"><i class="fas fa-angle-right"></i></a>
        <a href="?page=<%= totalPages %>&type=<%= type %>"><i class="fas fa-angle-double-right"></i></a>
        <% } %>
    </div>
</div>
<jsp:include page="footer.jsp" />
<script>
    // 删除确认
    function confirmDelete(id) {
        if (confirm('确定要删除这条内容吗？此操作无法撤销。')) {
            window.location.href = '<%= request.getContextPath() %>/content/delete?id=' + id;
        }
    }
    // 类型过滤
    function filterContents() {
        const type = document.getElementById('typeFilter').value;
        window.location.href = '?type=' + type;
    }
    // 搜索功能（提示）
    function searchContents() {
        const searchTerm = document.getElementById('searchInput').value;
        alert('搜索功能将在未来版本中实现');
    }
    // 显示操作结果提示
    <% if (request.getAttribute("actionResult") != null) {
        String actionResult = (String) request.getAttribute("actionResult");
        if ("addSuccess".equals(actionResult)) { %>
    alert("内容添加成功！");
    <% } else if ("updateSuccess".equals(actionResult)) { %>
    alert("内容更新成功！");
    <% } else if ("deleteSuccess".equals(actionResult)) { %>
    alert("内容删除成功！");
    <% } else if ("statusUpdateSuccess".equals(actionResult)) { %>
    alert("内容状态更新成功！");
    <% } %>
    <% } %>
</script>
</body>
</html>